main.controller('loginCtrl', ["$scope", "$rootScope", "$location", "tools", "$http", 'userConfig', "loginServ", function ($scope, $rootScope, $location, tools, $http, userConfig, loginServ) {
    $('.ui.checkbox').checkbox({
        checked: 'checked'
    });

    $scope.rememberPwd = !!window.localStorage.getItem('pwd');

    $scope.user = {
        userName: window.localStorage.getItem('userName') || '',
        pwd: window.localStorage.getItem('pwd'),
        password: ''
    };

    $scope.errInfo = "";

    $scope.login = function () {
        $scope.user.userName = tools.trim($scope.user.userName);
        $scope.user.pwd = tools.trim($scope.user.pwd);

        if ($scope.user.userName === '') {
            return $scope.errInfo = "用户名不能为空!";
        } else if ($scope.user.pwd === '') {
            return $scope.errInfo = "密码不能为空!";
        }

        $scope.user.password = hex_md5($scope.user.pwd);

        tools.get({
            url: '/Ibm-Client/login.htm',
            data: $scope.user,
            succ: function (resp) {
                if (resp.success) {
                    // 记住用户名
                    window.localStorage.setItem('userName', $scope.user.userName);
                    window.localStorage.setItem('userInfo', JSON.stringify(resp.value));
                    // console.log(window.localStorage.getItem('userType'));
                    // console.log(JSON.parse(window.localStorage.getItem('userInfo')), resp.value);

                    if (tools.mobileEnv()) {
                        var rememberPwd = $('.mobile-login-container').find('.checkbox.rememberPwd').hasClass('checked');
                    } else {
                        var rememberPwd = $('.login-container').find('.checkbox.rememberPwd').hasClass('checked');
                    }

                    if (rememberPwd) {
                        window.localStorage.setItem('pwd', $scope.user.pwd);
                    } else {
                        window.localStorage.setItem('pwd', "");
                    }

                    userConfig.initUserConfig(resp.value);
                    $rootScope.$broadcast('LOGIN_EV');

                    if (tools.mobileEnv()) {
                        $location.url('/mobilePages');
                    } else {
                        $location.url('/entry');
                    }
                } else {
                    $scope.errInfo = '用户名或者密码错误!';
                }
            }
        });
    }
    $scope.clear = function () {
        $scope.user.userName = $scope.user.pwd = $scope.errInfo = "";
    }

    loginServ.addLogo();
    loginServ.addSologn();
}]);
services.factory('loginServ', ["$rootScope", "tools", "$timeout", "$routeParams", function ($rootScope, tools, $timeout, $routeParams) {
    return {
        addLogo: function () {
            if (tools.isJiaguwen()) {
                $('.login-container .company-logo').addClass('jiaguwen').attr('src', '/trend-new-BI/assets/img/jiaguwen-logo.png');
            }
            if (tools.isQushu()) {
                $('.login-container .company-logo').addClass('qushu').attr('src', '/trend-new-BI/assets/img/qushu-logo.jpg');
            }
        },
        addSologn: function () {
            if (tools.isJiaguwen()) {
                $('.sologn-box').text('JGW-BI 2.0,让大数据大有不同');
                document.title = "JGW-BI 2.0";
            }
            if (tools.isQushu()) {
                $('.sologn-box').text('趋数BI 2.0 , 让大数据大有不同');
                document.title = "趋数BI 2.0";
            }
        }
    }
}]);